package caip2021p.rcv3;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] lineArr = sc.nextLine().split(" ");
        int n = Integer.parseInt(lineArr[0]);
        int m = Integer.parseInt(lineArr[1]);
        
        
        int[][] note = new int[n+1][m+1]; // 从1开始索引
        
        for (int i = 1; i <= n * m; ++i) {
            String s = sc.nextLine();
            int x = 1, y = 1;
            
            for (int j = 0; j < s.length(); ++j) {
                char c = s.charAt(j);
                switch (c) {
                    case '1':
                        y += 1;
                        y = Math.min(y, m);
                        break;
                    case '2':
                        x += 1;
                        x = Math.min(x, n);
                        break;
                    case '3':
                        y -= 1;
                        y = Math.max(1, y);
                        break;
                    case '4':
                        x -= 1;
                        x = Math.max(1, x);
                        break;
                    default:
                        if (note[x][y] == 0) {
                            note[x][y] = i;
                        }
                        break;
                }
            }
        }
        
        // 输出结果
        for (int i = 1; i <= n; ++i) {
            System.out.print(note[i][1]);
            for (int j = 2; j <= m; ++j) {
                System.out.print(" " + note[i][j]);
            }
            System.out.println();
        }
    }
}
